//
//  YSLDiscoverSearchView.swift
//  SinaWeibo
//
//  Created by 嵩雷 on 16/3/6.
//  Copyright © 2016年 嵩雷. All rights reserved.
//

import UIKit

class YSLDiscoverSearchView: UIView, UITextFieldDelegate {
    
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var searchField: UITextField!
    /**
     SearchField的右边约束
     */
    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    
//    提供类方法通过xib创建view   why?
   class func searchView() -> YSLDiscoverSearchView {

        let view = NSBundle.mainBundle().loadNibNamed("YSLDiscoverSearchView", owner: nil, options: nil).last! as! YSLDiscoverSearchView
        return view
    }
    
//    searchField左侧放大镜  懒加载控件
    private lazy var leftImage: UIImageView = {
        
        return UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
    }()
//    设置searchField坐标
    override func awakeFromNib() {
        leftImage.frame = CGRectMake(0, 0, frame.height, frame.height)
        leftImage.contentMode = .Center
        searchField.leftView = leftImage
        
        searchField.leftViewMode = .Always
    }
//    代理方法
    @IBAction func textFieldDidBeginEditing(sender: UITextField) {
        textFieldRightCons.constant = cancelBtn.frame.width
        //        执行动画
        UIView.animateWithDuration(0.25) { () -> Void in
            self.searchField.layoutIfNeeded()
        }
        
    }
    @IBAction func cancelButtonClick(sender: UIButton) {
        
        searchField.resignFirstResponder()
        textFieldRightCons.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.searchField.layoutIfNeeded()
        }
    }
}







